﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5.Grades
{
    public partial class newAddGrade : Form
    {
        public newAddGrade()
        {
            InitializeComponent();
           
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value = comboBox1.Text;
            comboBox2.DataSource = WJNDBEntities.dbhelp.Student.Where(o => o.CNo == comboBox1.Text.Trim()).ToList();
            comboBox2.DisplayMember = "SNo";

            comboBox3.DataSource = WJNDBEntities.dbhelp.Election.Where(o => o.CNo == comboBox1.Text.Trim()).ToList();
            comboBox3.DisplayMember = "LNo";
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new DataGridViewRow());
        }

        private void newAddGrade_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new DataGridViewRow());
            comboBox1.DataSource = WJNDBEntities.dbhelp.Class.ToList();
            comboBox1.DisplayMember = "CNo";
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Student s;
            dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value = comboBox2.Text;
            s = WJNDBEntities.dbhelp.Student.Where(o => o.SNo == comboBox2.Text.Trim()).FirstOrDefault();
            dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value = s.SName;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value = comboBox3.Text;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
          //  dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            DataGridViewRow dr = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex];
            dr.Selected = true;
            int i = dataGridView1.CurrentCell.ColumnIndex;
            int j = dataGridView1.CurrentCell.RowIndex;
            string sid = Convert.ToString(dataGridView1[0, j].Value).Trim();
            DialogResult MsgBoxResult;//设置对话框的返回值
            MsgBoxResult = MessageBox.Show("确定要删除第" + (j + 1) + "行内容？",
            "提示",//对话框的标题
            MessageBoxButtons.YesNo,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Question,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button2);
            if (MsgBoxResult == DialogResult.Yes)
            {
                try
                {
                    
                    dataGridView1.Rows.Remove(dr);
                    MessageBox.Show("删除成功！");

                }
                catch
                {
                    MessageBox.Show("删除失败！");
                }
                finally
                {
                  //  dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                }
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < dataGridView1.RowCount; i++)
            { 
                string sid = Convert.ToString(dataGridView1[1, i].Value).Trim();
                string lno = Convert.ToString(dataGridView1[3,i].Value).Trim();
                int score = Convert.ToInt32(dataGridView1[4, i].Value);
                MessageBox.Show(score+"!");
                Grade g = new Grade();
                g.SNo = sid;
                g.LNo = lno;
                g.Score = score;
                try
                {
                    WJNDBEntities.dbhelp.Grade.Add(g);
                    WJNDBEntities.dbhelp.SaveChanges();
                    MessageBox.Show("保存成功");
                }
                catch (Exception)
                {
                    MessageBox.Show("第"+(i+1)+"行保存失败");
                }
                
            }
        }
    }
}
